#!/usr/bin/env python
#  This program is free software.
#  You can redistribute it and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation.
#  That is either version 2 of the License, or if you prefer any later version.

import pygtk
import gtk
import gtk.glade
from Listener import *


class ClientInterface:
    """JAMS is an Awesome Media Streamer"""

    def __init__(self, listenerObj):

    	""" The next two lines of code change the glade interface file in to a XML file then load the attributes of it in to python. """
        self.gladefile = "ClientGUI.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        self.listenerObj = listenerObj

    	""" The next few lines involve matching up the event handlers with the methods they correspond to so if a button is click it pulls
		pulls up the relevant code. Also the final line connects the dictionary. """
        dic = { "on_BT_OK_clicked" : self.btnPlay_clicked,
                "on_BT_STOP_clicked" : self.btnStop_clicked,
                "on_MainWindow_destroy" : gtk.main_quit }
        self.wTree.signal_autoconnect(dic)

    def btnPlay_clicked(self, widget):
        """ This method takes in the relevant details to be used in connecting the stream. It will also called the play stream. """
        hostname = self.wTree.get_widget("HostIpEntry").get_text()
        localIP = self.wTree.get_widget("ClientIpEntry").get_text()
        port = int(self.wTree.get_widget("PortEntry").get_text())
        self.listenerObj.setServerDetails(localIP, port)
        server=socket.socket()
        server.connect((hostname, port))
        server.send("PLAY")
        listenerObj.playStream()

    def btnStop_clicked(self, widget):
        server.send("STOP")

if __name__ == "__main__":
    interface = ClientInterface()
    gtk.main()
